/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cda.TestMenu;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;

/**
 *
 * @author Shyzkanza
 */
public class Map {
    
    private int LARGEUR = 36;
    private int HAUTEUR = 13;
    
    private BitmapFont font;
    private Texture texture;
    private Sprite sprite;
    private int FRAME_COLS = 16;
    private int FRAME_ROWS = 8;
    private String str1;
    private TextureRegion currentFrame;
    TextureRegion[][] tmp;
    
    private int[][] _level = new int[37][13];
    int[][] map = {
        {0,0,0,0,0,0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0, 0, 0, 0, 0, 0, 0,0, 0,0,0, 0, 0, 0, 0, 0, 7,7},
        {0,0,0,0,0,0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0, 0, 0, 0, 0, 0, 0,0, 0,0,0, 0, 0, 0, 0, 0, 7,7},
        {0,0,0,0,0,0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0, 0, 0, 0, 0, 0, 0,0, 0,0,0, 0, 0, 0, 0, 0, 7,7},
	{0,0,0,0,0,0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0, 0, 0, 0,17, 0, 0,0, 0,0,0, 0, 0, 0, 0, 0, 7,7},
	{0,0,0,0,0,0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0, 0, 0, 0, 0, 0, 0,0, 0,0,0, 0, 0, 0, 0, 0, 7,7},
	{0,0,0,0,0,0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0, 0, 0, 0, 0, 0, 0,0, 0,0,0, 0, 0, 0, 0, 0, 7,7},
	{0,0,0,0,0,0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0, 0, 0, 0, 0, 0, 0,0, 0,0,0, 0, 0, 0, 0, 0, 7,7},
	{0,0,0,0,0,0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,18,0,0, 0,16,17,16,17,16,0, 0,0,0, 0, 0, 0, 0, 0, 7,7},
        {0,0,0,0,0,0,0,17,16,17,16,17,16, 0, 0, 0, 0,0,0, 0, 0, 0, 0, 0, 0,0, 0,0,0, 0, 0, 0, 0, 0, 7,7},
        {0,0,0,0,0,0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0, 0, 0, 0, 0, 0, 0,0, 0,0,0, 0, 0, 0, 0, 0, 7,7},
	{0,0,0,0,0,0,0, 0, 0, 0, 0,64,65,65,65,66, 0,0,0, 0, 0, 0, 0,64,66,0, 0,0,0, 0, 0, 0, 0, 0, 7,7},
	{2,2,2,2,2,2,2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,2,2, 2, 2, 2, 2, 2, 2,2, 2,2,2, 2, 2, 2, 2, 2, 2,7},
        {7,7,7,7,7,7,7,7,7,7, 7, 7, 7, 7, 7, 7, 7, 7,7,7, 7, 7, 7, 7, 7, 7,7, 7,7,7, 7, 7, 7, 7, 7, 7,7}
        };
    
    
    
    
    public Map(){
        
        for (int i = 0; i < LARGEUR; i++)
            for (int j = 0; j < HAUTEUR; j++)
            {
                _level[i][j] = map[j][i];
            }
        
        texture = new Texture(Gdx.files.internal("mapp.png"));       
    }

    public void resize(int width, int height) {
        
    }
    
    public Boolean Collide(Vector2 position){
        Boolean res = true;

        // conversion des coordonnées
        int i = ((int)(position.x)) /15, j = (int) (position.y / 15);

        if (i >= 0 && j >= 0 && i < LARGEUR && j < HAUTEUR)
            res = onlyMap(i,j);

        return res;
    }
    
    public void Draw(SpriteBatch spriteBatch){
        for (int i = 0; i < LARGEUR; i++)
        {
            for (int j = 0; j < HAUTEUR; j++)
            {
                drawBlock(spriteBatch, i, j);
            }
        }
    }
    
    private boolean onlyMap(int i, int j){
        return (_level[i][j] != 2) && (_level[i][j] != 17) && (_level[i][j] != 16);
    }

    /*public void render() {
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
        
        str1 = "Largeur : "+Gdx.graphics.getWidth()+" - Hauteur : "+Gdx.graphics.getHeight();
        
        stateTime += Gdx.graphics.getDeltaTime();
        currentFrame = walkAnimationDroite.getKeyFrame(stateTime, true);
        
        spriteBatch.begin();
        
        spriteBatch.draw(currentFrame, 50, 50);
        font.draw(spriteBatch, str1, 1, 40);
        spriteBatch.end();
    }*/
    
    private void drawBlock(SpriteBatch sb, int x, int y){
        
        //Bloc à afficher
        int tile = _level[x][y];
        
        sprite = new Sprite(texture, (tile%16)*16, (tile/16)*16, 16, 16);
        
        sb.draw(sprite, x*16,Gdx.graphics.getHeight() - y*16);
        
    }
}
